package io.kwdb.examples;

import io.kwdb.sdk.*;
import java.util.*;

/** Verify that ILP encoder always adds trailing newline */
public class TestTrailingNewline {
  public static void main(String[] args) {
    IlpEncoder encoder = new IlpEncoder();

    System.out.println("Testing trailing newline compliance");
    System.out.println("=====================================\n");

    // Test 1: Single point
    Point point =
        Point.builder("sdk_bench")
            .tag("host", "test")
            .field("usage", 1.0)
            .timestamp(1696176000000000000L)
            .build();

    String encoded = encoder.encode(point);
    System.out.println("1. Single point encoding:");
    System.out.println("   Encoded: " + encoded.replace("\n", "\\n"));
    System.out.println("   Ends with \\n: " + encoded.endsWith("\n") + " ✓");
    System.out.println();

    // Test 2: Batch of 3 points
    List<Point> points = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
      points.add(
          Point.builder("sdk_bench")
              .tag("host", "batch-" + i)
              .field("usage", i * 10.0)
              .timestamp(1696176000000000000L + i)
              .build());
    }

    String batchEncoded = encoder.encode(points);
    System.out.println("2. Batch encoding (3 points):");
    System.out.println("   Encoded:\n" + batchEncoded.replace("\n", "\\n\n   "));
    System.out.println("   Ends with \\n: " + batchEncoded.endsWith("\n") + " ✓");
    System.out.println(
        "   Line count: "
            + batchEncoded.split("\n", -1).length
            + " (should be 4: 3 lines + 1 empty after last \\n)");
    System.out.println();

    // Test 3: Verify each line ends with \n
    String[] lines = batchEncoded.split("\n", -1);
    System.out.println("3. Line-by-line verification:");
    for (int i = 0; i < lines.length - 1; i++) { // -1 because last element after split is empty
      System.out.println("   Line " + (i + 1) + ": " + lines[i]);
    }
    System.out.println();

    System.out.println("✓ All trailing newline requirements met!");
    System.out.println("  - Single point: ends with \\n");
    System.out.println("  - Batch: each line ends with \\n");
    System.out.println("  - Compliant with ILP protocol portability requirements");
  }
}
